//************************************************************************
//Developed by Guo Ming Li 2009/2010
//Source code:
//  The source code may be freely modified or used but this credit section may not be removed 
//  The original author must be notified of any modifications plus a copy of 
//  the changed source code
//Contact info: mailto://gming.li@gmail.com
//************************************************************************
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.BizTalk.ExplorerOM;

namespace GML.BizTalk.OperationManager.Library
{
    public class SendPortGroups
    {
        public static void DeleteSendPortGroups(string btsDbServerName, string btsMgntDbName, string sBtsAppName)
        {
            BtsCatalogExplorer root = new BtsCatalogExplorer();
            List<SendPortGroup> sendPortGroups = new List<SendPortGroup>();

            try
            {
                root.ConnectionString = Helper.GetConnectionString(btsDbServerName, btsMgntDbName);

                foreach (SendPortGroup sendPortGroup in root.SendPortGroups)
                {
                    sendPortGroups.Add(sendPortGroup);
                }
                // remove a send port group
                foreach (SendPortGroup sendPortGroup in sendPortGroups)
                {
                    if (sendPortGroup.Application.Name == sBtsAppName)
                    {
                        root.RemoveSendPortGroup(sendPortGroup);
                    }
                }

                //try to commit the changes we made so far. 
                root.SaveChanges();
            }
            catch (Exception e)//If it fails, roll-back everything we have done so far
            {
                System.Diagnostics.Debug.WriteLine(e.Message + Environment.NewLine + e.StackTrace);
                Logger.doLog(Guid.NewGuid().ToString(), e.Message, Logger.ServerityLevel.ERROR);
                Logger.doLog(Guid.NewGuid().ToString(), e.StackTrace, Logger.ServerityLevel.ERROR);
                root.DiscardChanges();
                throw e;
            }
            finally
            {
                if (root != null) root.Dispose();
                root = null;
                sendPortGroups = null;
            }
        }

        public static bool StartAllSendPortGroups(string btsDbServerName, string btsMgntDbName, string sBtsAppName, out Exception ex)
        {
            try
            {
                SendPortGroupSetStatus(btsDbServerName, btsMgntDbName, sBtsAppName, PortStatus.Started, null);
                ex = null;
                return true;
            }
            catch (Exception e)
            {
                ex = e;
                return false;
            }
        }

        public static bool StopAllSendPortGroups(string btsDbServerName, string btsMgntDbName, string sBtsAppName, out Exception ex)
        {
            try
            {
                SendPortGroupSetStatus(btsDbServerName, btsMgntDbName, sBtsAppName, PortStatus.Stopped, null);
                ex = null;
                return true;
            }
            catch (Exception e)
            {
                ex = e;
                return false;
            }
        }

        public static void SendPortGroupSetStatus(string btsDbServerName, string btsMgntDbName, string sBtsAppName, PortStatus status, List<string> sendGroupNames)
        {
            BtsCatalogExplorer root = new BtsCatalogExplorer();
            List<SendPortGroup> sendPortGroups = new List<SendPortGroup>();

            try
            {
                root.ConnectionString = Helper.GetConnectionString(btsDbServerName, btsMgntDbName);

                if (sendGroupNames == null)
                {
                    foreach (SendPortGroup sendPortGroup in root.SendPortGroups)
                    {
                        sendPortGroup.Status = status;
                    }
                }
                else
                {
                    foreach (SendPortGroup sendPortGroup in root.SendPortGroups)
                    {
                        foreach (string spGroupName in sendGroupNames)
                        {
                            if (spGroupName.Equals(sendPortGroup.Name))
                            {
                                sendPortGroup.Status = status;
                            }
                        }
                    }
                }

                //try to commit the changes we made so far. 
                root.SaveChanges();
            }
            catch (Exception e)//If it fails, roll-back everything we have done so far
            {
                System.Diagnostics.Debug.WriteLine(e.Message + Environment.NewLine + e.StackTrace);
                Logger.doLog(Guid.NewGuid().ToString(), e.Message, Logger.ServerityLevel.ERROR);
                Logger.doLog(Guid.NewGuid().ToString(), e.StackTrace, Logger.ServerityLevel.ERROR);
                root.DiscardChanges();
                throw e;
            }
            finally
            {
                if (root != null) root.Dispose();
                root = null;
                sendPortGroups = null;
            }
        }


    }
}
